package com.skyline.spot.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.spot.dao.InviteDao;
import com.skyline.spot.mapper.InvitationMapper;
import com.skyline.spot.model.Invitation;
import com.skyline.user.dao.FanDao;
import com.skyline.user.dao.impl.FanDaoImpl;
import com.skyline.user.model.Fan;

/**
 * 
 * @author Colonel
 * 
 */
@Repository("inviteDao")
public class InviteDaoImpl extends BaseDaoImpl implements InviteDao {

	@Value("${inviteDao.insertInvitation}")
	private String insertInvitationSQL;

	@Value("${inviteDao.queryInvitationById}")
	private String queryInvitationByIdSQL;

	@Value("${inviteDao.queryInvitationByOwerIdAndActivity}")
	private String queryInvitationByOwerIdAndActivitySQL;

	@Value("${inviteDao.queryInvitationByOwerId}")
	private String queryInvitationByOwerIdSQL;

	@Value("${inviteDao.queryInvitationByOwerIdTimestamp}")
	private String queryInvitationByOwerIdTimestampSQL;

	@Value("${inviteDao.queryInvitationByOwerIdTimestampAndActivity}")
	private String queryInvitationByOwerIdTimestampAndActivitySQL;

	@Value("${inviteDao.queryInvitationsOfFanByFanId}")
	private String queryInvitationsOfFanByFanIdSQL;

	@Value("${inviteDao.queryInvitationsOfFanByFanId}")
	private String queryInvitationsOfFanByFanIdAndActivitySQL;

	@Value("${inviteDao.queryInvitationsOfFanByFanNickname}")
	private String queryInvitationsOfFanByFanNicknameSQL;

	@Value("${inviteDao.queryInvitationsOfFanByFanNicknameAndActivity}")
	private String queryInvitationsOfFanByFanNicknameAndActivitySQL;

	@Value("${inviteDao.queryInvitationsBySpotId}")
	private String queryInvitationsBySpotIdSQL;

	@Value("${inviteDao.queryInvitationsBySpotIdAndActivity}")
	private String queryInvitationsBySpotIdAndActivitySQL;

	@Value("${inviteDao.queryInvitationsBySpotName}")
	private String queryInvitationsBySpotNameSQL;

	@Value("${inviteDao.queryInvitationsBySpotNameAndActivity}")
	private String queryInvitationsBySpotNameAndActivitySQL;

	@Value("${inviteDao.queryInvitationsByAppointmentTime}")
	private String queryInvitationsByAppointmentTimeSQL;

	@Value("${inviteDao.queryInvitationsByAppointmentTimeAndActivity}")
	private String queryInvitationsByAppointmentTimeAndActivitySQL;

	@Value("${inviteDao.deleteInvitationById}")
	private String deleteInvitationByIdSQL;

	@Value("${inviteDao.deleteInvitationByOwerId}")
	private String deleteInvitationByOwerIdSQL;

	@Value("${inviteDao.deleteInvitationByTimestamp}")
	private String deleteInvitationByTimestampSQL;

	@Value("${inviteDao.deleteInvitationBySpotId}")
	private String deleteInvitationBySpotIdSQL;

	@Value("${inviteDao.deleteAll}")
	private String deleteAllSQL;

	@Value("${inviteDao.deleteInvitationByFanId}")
	private String deleteInvitationByFanIdSQL;

	@Value("${inviteDao.deleteInvitationByFanIdOwerId}")
	private String deleteInvitationByFanIdOwerIdSQL;

	@Value("${inviteDao.deleteInvitationByFanIdOwerIdTimestamp}")
	private String deleteInvitationByFanIdOwerIdTimestampSQL;

	@Value("${inviteDao.updateInvitationByOwerIdAndTimestamp}")
	private String updateInvitationByOwerIdAndTimestampSQL;

	/********************** insert *****************************/

	@Override
	public long insertInvitation(long owerId, long fanId, String fanNickname, String fanPortait,
			long spotId, String spotName, String content, Date invitingTime, Date appointmentTime) {

		return this.insertWithIdReturn(insertInvitationSQL, owerId, fanId, fanNickname, spotId,
				spotName, content, invitingTime, appointmentTime);
	}

	/********************** query ****************************/

	@Override
	public Invitation queryInvitationById(long id) {
		List<Invitation> result = jdbcTemplate.query(queryInvitationByIdSQL,
				InvitationMapper.getMapper(), id);
		if (result == null) {
			return null;
		} else {
			return result.get(0);
		}
	}

	@Override
	public List<Invitation> queryInvitationByOwerId(long owerId, Activity activity) {
		List<Invitation> result = null;
		if (activity != null) {
			result = jdbcTemplate.query(queryInvitationByOwerIdAndActivitySQL,
					InvitationMapper.getMapper(), owerId, activity.toString());
		} else {
			result = jdbcTemplate.query(queryInvitationByOwerIdSQL, InvitationMapper.getMapper(),
					owerId);
		}

		return result;
	}

	@Override
	public List<Invitation> queryInvitationByOwerId(long owerId, Date timestamp, Activity activity) {
		List<Invitation> result = null;
		if (activity != null) {
			result = jdbcTemplate.query(queryInvitationByOwerIdTimestampAndActivitySQL,
					InvitationMapper.getMapper(), owerId, activity);
		} else {
			result = jdbcTemplate.query(queryInvitationByOwerIdTimestampSQL,
					InvitationMapper.getMapper(), owerId);
		}
		return result;
	}

	@Override
	public List<Fan> queryFansByOwerIdTimestamp(long owerId, Date timestamp, Activity activity) {
		List<Invitation> result = this.queryInvitationByOwerId(owerId, timestamp, activity);
		List<Fan> fans = new ArrayList<Fan>();
		FanDao fanDao = new FanDaoImpl();
		List<Fan> tempFans = fanDao.queryFansByIdolId(owerId, null, activity);

		for (Invitation in : result) {
			long fanId = in.getFanId();
			for (Fan fan : tempFans) {
				if (fan.getFanId() == fanId) {
					fans.add(fan);
					break;
				}
			}
		}
		return fans;
	}

	@Override
	public List<Invitation> queryInvitationsOfFanByFanId(long fanId, Activity activity) {
		List<Invitation> result = null;

		if (activity != null) {
			result = jdbcTemplate.query(queryInvitationsOfFanByFanIdAndActivitySQL,
					InvitationMapper.getMapper(), fanId, activity);
		} else {
			result = jdbcTemplate.query(queryInvitationsOfFanByFanIdSQL,
					InvitationMapper.getMapper(), fanId);
		}

		return result;
	}

	@Override
	public List<Invitation> queryInvitationsOfFanByFanNickname(String nickname, Activity activity) {
		List<Invitation> result = null;

		if (activity != null) {
			result = jdbcTemplate.query(queryInvitationsOfFanByFanNicknameAndActivitySQL,
					InvitationMapper.getMapper(), nickname, activity);
		} else {
			result = jdbcTemplate.query(queryInvitationsOfFanByFanNicknameSQL,
					InvitationMapper.getMapper(), nickname);
		}

		return result;
	}

	@Override
	public List<Invitation> queryInvitationsBySpotId(long spotId, Activity activity) {
		List<Invitation> result = null;

		if (activity != null) {
			result = jdbcTemplate.query(queryInvitationsBySpotIdSQL, InvitationMapper.getMapper(),
					spotId, activity);
		} else {
			result = jdbcTemplate.query(queryInvitationsBySpotIdAndActivitySQL,
					InvitationMapper.getMapper(), spotId);
		}
		return result;
	}

	@Override
	public List<Invitation> queryInvitationsBySpotName(String spotName, Activity activity) {
		List<Invitation> result = null;

		if (activity != null) {
			result = jdbcTemplate.query(queryInvitationsBySpotNameSQL,
					InvitationMapper.getMapper(), spotName, activity);
		} else {
			result = jdbcTemplate.query(queryInvitationsBySpotNameAndActivitySQL,
					InvitationMapper.getMapper(), spotName);
		}

		return result;
	}

	@Override
	public List<Invitation> queryInvitationsByAppointmentTime(Date appointmentTime,
			Activity activity) {
		List<Invitation> result = null;

		if (activity != null) {
			result = jdbcTemplate.query(queryInvitationsByAppointmentTimeSQL,
					InvitationMapper.getMapper(), appointmentTime, activity);
		} else {
			result = jdbcTemplate.query(queryInvitationsByAppointmentTimeAndActivitySQL,
					InvitationMapper.getMapper(), appointmentTime);
		}

		return result;
	}

	/********************************** delete **********************************/

	@Override
	public void deleteInvitationById(long id) {
		jdbcTemplate.update(deleteInvitationByIdSQL, Activity.DELETED.toString(), id);
	}

	@Override
	public void deleteInvitationByOwerId(long owerId) {
		jdbcTemplate.update(deleteInvitationByOwerIdSQL, Activity.DELETED.toString(), owerId);
	}

	@Override
	public void deleteInvitationByTimestamp(long owerId, Date timestamp) {
		jdbcTemplate.update(deleteInvitationByTimestampSQL, Activity.DELETED.toString(), owerId,
				timestamp);
	}

	@Override
	public void deleteInvitationBySpotId(long spotId) {
		jdbcTemplate.update(deleteInvitationBySpotIdSQL, Activity.DELETED.toString(), spotId);
	}

	@Override
	public void deleteAll() {
		jdbcTemplate.update(deleteAllSQL);
	}

	@Override
	public void deleteInvitationByFanId(long owerId, long fanId, Date timestamp) {
		if (owerId == -1) {
			jdbcTemplate.update(deleteInvitationByFanIdSQL, fanId);
		} else if (owerId >= 0 && timestamp != null) {
			jdbcTemplate
					.update(deleteInvitationByFanIdOwerIdTimestampSQL, fanId, owerId, timestamp);
		} else if (owerId >= 0 && timestamp == null) {
			jdbcTemplate.update(deleteInvitationByFanIdOwerIdSQL, fanId, owerId);
		}
	}

	/********************** update ****************************************/

	@Override
	public void updateInvitation(long owerId, long fanId) {

	}

	@Override
	public void updateInvitationByOwerIdAndTimestamp(Long owerId, long spotId, String spotName,
			Date timestamp, Date appointmentTime) {
		jdbcTemplate.update(updateInvitationByOwerIdAndTimestampSQL, spotId, spotName,
				appointmentTime, owerId, timestamp);
	}

}
